/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from "react";
import { connect } from "dva";
import { Form } from "antd";
import { useAntdTable } from "ahooks";
import moment from "moment/moment";
import Search from "./components/Search/index";
import Tables from "./components/Tables/index";

const time_format = "YYYY-MM-DD HH:mm:ss";

const Setting = props => {
  const { dispatch, form } = props;
  const getTableData = (res, formData) => {
    const { time, brand, inn_name, ...rest } = formData;
    const [start, end] = time;
    return dispatch({
      type: "order_calculate/getTableList",
      payload: {
        ...res,
        ...rest,
        page: res.current,
        start_date: moment(start).format(time_format),
        end_date: moment(end).format(time_format),
        brand: brand === "all" ? "" : brand,
        inn_name: inn_name === "all" ? "" : inn_name || "",
      },
    }).then(dataList => dataList);
  };
  const { search, loading, tableProps } = useAntdTable(getTableData, {
    defaultPageSize: 20,
    form,
  });
  const { submit } = search;
  return (
    <div>
      <Search form={form} parentHandle={search} />
      <Tables loading={loading} {...tableProps} POST={dispatch} refreshPage={submit} />
    </div>
  );
};

export default connect(result => {
  const { loading } = result;
  return {
    loading,
  };
})(Form.create()(Setting));
